<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\adminapi\logic\pop;


use app\common\model\pop\AddDesktop;
use app\common\logic\BaseLogic;
use app\common\model\template\Template;
use app\common\service\FileService;
use think\facade\Db;


/**
 * AddDesktop逻辑
 * Class AddDesktopLogic
 * @package app\adminapi\logic
 */
class AddDesktopLogic extends BaseLogic
{


    /**
     * @notes 添加
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/02/23 16:43
     */
    public static function add(array $params): bool
    {
        Db::startTrans();
        try {
            $addDesktop = AddDesktop::where(['platform'=>$params['platform'],'type'=>$params['type']])->findOrEmpty();
            if (!$addDesktop->isEmpty()){
                self::setError('已存在该类型加桌弹窗');
                return false;
            }
            $template = Template::where('id',$params['template_id'])->findOrEmpty();
            AddDesktop::create([
                'template_id' => $params['template_id'],
                'admin_id' => $params['admin_id'],
                'platform' => $params['platform'],
                'name' => $params['name'],
                'image' => $template->image??'',
                'remark' => $params['remark']??'',
                'type' => $params['type'],
                'is_default' => $params['is_default']??1,
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 编辑
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/02/23 16:43
     */
    public static function edit(array $params): bool
    {
        Db::startTrans();
        try {
//            self::clearDefault($params['is_default'],$params['platform']);
            $template = Template::where('id',$params['template_id'])->findOrEmpty();
            $data = [
                'name' => $params['name'],
                'template_id' => $params['template_id'],
                'remark' => $params['remark']??'',
                'type' => $params['type'],
            ];
            if (!$template->isEmpty()){
                $data['image'] = $template->image??'';
            }
            AddDesktop::where('id', $params['id'])->update($data);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }

    public static function clearDefault($is_default,$platform=0)
    {
        if ($is_default==1){
            AddDesktop::where('platform',$platform)->update(['is_default'=>0]);
        }
        return true;
    }


    /**
     * @notes 删除
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/02/23 16:43
     */
    public static function delete(array $params): bool
    {
        $result = AddDesktop::where('id',$params['id'])->find();
        if ($result['is_default']==1){
            self::setError('默认模板无法删除');
            return false;
        }
        return AddDesktop::destroy($params['id']);
    }


    /**
     * @notes 获取详情
     * @param $params
     * @return array
     * @author likeadmin
     * @date 2024/02/23 16:43
     */
    public static function detail($params): array
    {
        return AddDesktop::findOrEmpty($params['id'])->toArray();
    }
}